<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;



class LoginController extends Controller
{
    //后台登录页面展示
    public function index(){
        if(auth()->check()){
            return redirect(route('admin.index'));
        }
       return view('admin.admin.index');
    }

    //后台登录处理
    public function login(Request $request){
           $data=$this->validate($request,[
                'username'=>'required',
                'password'=>'required'

           ],[
               'username.required'=>'用户名不能为空',
               'password.required'=>'密码不能为空'
           ]);

        //    判断账号是否存在
          if(auth()->attempt($data)){
             
              auth()->user()->update(['last_ip'=>$request->ip()]);
              return view('admin.admin.adminindex');
          }else{
            return redirect()->back()->with('error','账号或者密码错误');

          };
             
          
    }

    


}
